package com.utils.adapter;

import android.content.Context;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by mac_wk on 14-12-11.
 */
public class ViewHolder {
    private final SparseArray<View> mViews;
    private View mConvertView;
    public ViewHolder( Context context,int layout_id,ViewGroup parent) {
        this.mViews = new SparseArray<View>();
        mConvertView = LayoutInflater.from(context).inflate(layout_id,parent, false);
        //setTag
        mConvertView.setTag(this);
    }
    public static ViewHolder get(Context context, View convertView,
                                 ViewGroup parent, int layoutId, int position)
    {
        if (convertView == null)
        {
            return new ViewHolder(context, layoutId,parent);
        }
        return (ViewHolder) convertView.getTag();
    }
    public <T extends View> T getView(int viewId)
    {

        View view = mViews.get(viewId);
        if (view == null)
        {
            view = mConvertView.findViewById(viewId);
            mViews.put(viewId, view);
        }
        return (T) view;
    }

    public View getConvertView()
    {
        return mConvertView;
    }
}
